package Tech.yunjing.onlineStorage.service;

import Tech.yunjing.onlineStorage.common.utils.Constants;
import Tech.yunjing.onlineStorage.handler.FileHandler;
import Tech.yunjing.onlineStorage.mapper.TechFileMapper;
import Tech.yunjing.onlineStorage.module.TechFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by lenovo on 2016/6/28.
 */
@Service
public class TechFileService {

    @Value("${disk_path}")
    private String disk_path;

    @Autowired
    private TechFileMapper techFileMapper;
    /**
     * 根据父级id查找目录
     */
    public String findPath(String folderId){
        return techFileMapper.findPath(folderId);
    }

    public TechFile getById(String id){
        return techFileMapper.getById(id);
    }

    public void save(TechFile techFile){
        techFileMapper.save(techFile);
    }


    public void save(TechFile techFile,String parentPath){
        techFile.setId(UUID.randomUUID().toString());
        techFile.setPath(parentPath+UUID.randomUUID().toString().replace("-","")+"/");
        techFile.setType(Constants.RESOURCE_TYPE_FOLDER);
        techFileMapper.save(techFile);
        //db 保存成功 生成 文件目录
        FileHandler.newFolder(disk_path+techFile.getPath());
    }

    public void physicallyDelete(String id){
        techFileMapper.physicallyDelete(id);
    }

    public boolean update(TechFile techFile){
        techFileMapper.update(techFile);
        return true;
    }

    public boolean rename(TechFile techFile){
        techFileMapper.rename(techFile);
        return true;
    }

    public boolean move(String resourceId,String fromId,String targetId){
        TechFile resource =getById(resourceId);
        TechFile from = getById(fromId);
        techFileMapper.move(resourceId,fromId,targetId);

        return false;
    }
}
